#include "device_manager.h"
#include "esp_log.h"
#include "esp_idf_version.h"

#include "config.h"
#include "commu.h"
#ifndef USE_SYS_MEM
#include "m_memory.h"
extern uint8_t memory_pool[16 * 1024];
extern MemoryPoolManager pool;
#endif
static const char *TAG = "device manager";

char *get_version(void)
{
    return esp_get_idf_version();
}

uint8_t process_device_manager_cmd(protocol_packet_t *pkt)
{
    switch (pkt->subcmd) {
        case DEVICE_MANAGER_GET_VERSION:
            char * version = get_version();
            uint8_t len = strlen(version);
            protocol_packet_t *send = protocol_build_packet(SWAP_HIGH_LOW(pkt->srcdest), pkt->cmd, pkt->subcmd, (uint8_t *)version, len);
            SendToCommu((uint8_t *)send, get_protocol_packet_size(send));
#ifdef USE_SYS_MEM
            free(send);
#else
            mempool_free(&pool, send);
#endif
            break;
        default:
            ESP_LOGI(TAG, "UNKNOW DEVICE MANAGER");
            break;
    }
    return 0;
}
